package com.chuji.crmcj.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.GvDialogAda;

import java.util.LinkedHashMap;
import java.util.Map;

public class SimpleBottomDialog extends Dialog {

    private Context context;
    private GridView mGvDialog;
    private int lable = -1;
    private TextView mTvPayment;

    public static final Map<String, Double> jiaoqiang = new LinkedHashMap<String, Double>() {
        {
            put("家用6座以下", 950.00);
            put("家用6座以上", 1100.00);
        }
    };

    private static final Map<String, Double> huahen = new LinkedHashMap<String, Double>() {
        {
            put("2千", 400.00);
            put("5千", 570.00);
            put("1万", 760.00);
            put("2万", 1140.00);
        }
    };

    private static final Map<String, Double> third = new LinkedHashMap<String, Double>() {
        {
            put("5万", 516.00);
            put("10万", 746.00);
            put("20万", 924.00);
            put("50万", 1252.00);
            put("100万", 1630.00);
        }
    };

    private static final Map<String, Double> boli = new LinkedHashMap<String, Double>() {
        {
            put("国产", 0.0015);
            put("进口", 0.0025);
        }
    };


    public SimpleBottomDialog(Context context) {
        this(context, R.style.Theme_Dialog_From_Bottom);
    }

    public SimpleBottomDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
        init();
    }

    public SimpleBottomDialog(int lable, Context mActivity) {
        this(mActivity, R.style.Theme_Dialog_From_Bottom);
        this.lable = lable;
    }

    private void init() {
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_simple_bottom);

        initViews();
        initValues();
    }

    private Map<String, Double> map;

    private void initViews() {
        mGvDialog = (GridView) findViewById(R.id.gv_dialog);
        mTvPayment = (TextView) findViewById(R.id.tv_payment);

        switch (lable) {
            case 0:
                map = jiaoqiang;
                mTvPayment.setText("请选择座位数");
                break;
            case 1:
                map = huahen;
                break;
            case 2:
                map = third;
                break;
            case 3:
                map = boli;
                mTvPayment.setText("请选择玻璃产地");
                break;
            default:
                break;
        }

        GvDialogAda gvDialogAda = new GvDialogAda(map);
        mGvDialog.setAdapter(gvDialogAda);
        mGvDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onOKClickListener != null) {
                    onOKClickListener.onOKClick(mGvDialog, position, map, lable);
                    System.out.println("map");
                }
                dismiss();
            }
        });
    }

    private void initValues() {
        // 不能写在init()中  
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        lp.width = dm.widthPixels;//让dialog的宽占满屏幕的宽  
        lp.gravity = Gravity.BOTTOM;//出现在底部
        window.setAttributes(lp);
    }

//    View.OnClickListener clickListener = new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//            // TODO Auto-generated method stub
//            switch (v.getId()) {
//                case R.id.btn_0:
//                    if (onOKClickListener != null) {
//                        onOKClickListener.onOKClick(v);
//                    }
//                    dismiss();
//                    break;
//                case R.id.btn_1:
//                    dismiss();
//                break;
//
//                default:
//                    break;
//            }
//        }
//    };

    private OnOKClickListener onOKClickListener;

    public interface OnOKClickListener {
        public void onOKClick(View v, int position, Map<String, Double> map, int lable);
    }

    public void setOnOKClickListener(OnOKClickListener onOKClickListener) {
        this.onOKClickListener = onOKClickListener;
    }
}